

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Example: resources &mdash; Playdoh v0.3.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Playdoh v0.3.1 documentation" href="index.html" />
    <link rel="up" title="Examples" href="examples.html" />
    <link rel="next" title="Playdoh Reference" href="reference.html" />
    <link rel="prev" title="Example: allocation" href="examples-allocation.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="reference.html" title="Playdoh Reference"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="examples-allocation.html" title="Example: allocation"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Playdoh v0.3.1 documentation</a> &raquo;</li>
          <li><a href="examples.html" accesskey="U">Examples</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="example-resources">
<span id="id1"></span><h1>Example: resources<a class="headerlink" href="#example-resources" title="Permalink to this headline">¶</a></h1>
<p>Resource allocation example showing how to allocate resources on the servers.
The Playdoh server must run on the local machine and on the default port
(2718 by default)
for this script to work.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">playdoh</span> <span class="kn">import</span> <span class="o">*</span>


<span class="c"># It can also be a list of server IP addresses</span>
<span class="n">servers</span> <span class="o">=</span> <span class="s">&#39;localhost&#39;</span>

<span class="c"># Get all the allocated resources on the servers</span>
<span class="c"># total_resources[0][&#39;CPU&#39;] is a dictionary where keys are client IP</span>
<span class="c"># addresses and values are the number of CPUs allocated to the corresponding</span>
<span class="c"># clients</span>
<span class="n">total_resources</span> <span class="o">=</span> <span class="n">get_server_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Total allocated resources:&quot;</span><span class="p">,</span> <span class="n">total_resources</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&#39;CPU&#39;</span><span class="p">]</span>

<span class="c"># Get the idle resources on the specified servers</span>
<span class="c"># idle_resources[0][&#39;CPU&#39;] is the number of CPUs available on the first server</span>
<span class="c"># This number includes the already allocated resources for this client</span>
<span class="n">idle_resources</span> <span class="o">=</span> <span class="n">get_available_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> idle CPUs&quot;</span> <span class="o">%</span> <span class="n">idle_resources</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&#39;CPU&#39;</span><span class="p">]</span>

<span class="c"># Get the resources allocated to this client on the specified servers</span>
<span class="c"># my_resources[&#39;CPU&#39;] is the number of CPUs allocated on the servers for this</span>
<span class="c"># client</span>
<span class="n">my_resources</span> <span class="o">=</span> <span class="n">get_my_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> CPUs allocated to me&quot;</span> <span class="o">%</span> <span class="n">my_resources</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&#39;CPU&#39;</span><span class="p">]</span>

<span class="c"># Allocate as many CPUs as possible on the specified servers for this client</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">request_all_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">,</span> <span class="s">&#39;CPU&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Just allocated </span><span class="si">%d</span><span class="s"> CPUs on the server&quot;</span> <span class="o">%</span> <span class="n">n</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

<span class="n">my_resources</span> <span class="o">=</span> <span class="n">get_my_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> CPUs allocated to me now&quot;</span> <span class="o">%</span> <span class="n">my_resources</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&#39;CPU&#39;</span><span class="p">]</span>

<span class="n">total_resources</span> <span class="o">=</span> <span class="n">get_server_resources</span><span class="p">(</span><span class="n">servers</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Total allocated resources now:&quot;</span><span class="p">,</span> <span class="n">total_resources</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&#39;CPU&#39;</span><span class="p">]</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="examples-allocation.html"
                        title="previous chapter">Example: allocation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="reference.html"
                        title="next chapter">Playdoh Reference</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/examples-resources.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="reference.html" title="Playdoh Reference"
             >next</a> |</li>
        <li class="right" >
          <a href="examples-allocation.html" title="Example: allocation"
             >previous</a> |</li>
        <li><a href="index.html">Playdoh v0.3.1 documentation</a> &raquo;</li>
          <li><a href="examples.html" >Examples</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2011, Cyrille Rossant, Bertrand Fontaine, Dan Goodman.
      Last updated on Aug 17, 2011.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.6.
    </div>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-6773961-3']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </body>
</html>